<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="用户名称" name="username">
						<a-select
							show-search
							option-filter-prop="label"
							:options="userOptions"
							v-model:value="searchFormState.username"
							placeholder="请输入用户名称"
					/></a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button class="xn-mg08" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 1500, y: 500 }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen(false, total)">
						<template #icon><plus-outlined /></template>
						新增订单
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'create_time'">
					{{ dayjs(record.create_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'modified_time'">
					{{ dayjs(record.modified_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'start_time'">
					{{ dayjs(record.start_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'end_time'">
					{{ dayjs(record.end_time).format('YYYY-MM-DD HH:mm:ss') }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag :color="{ 1: 'processing', 2: 'success', 3: 'error' }[record.status]">{{
						{ 1: '创建中', 2: '生效', 3: '过期' }[record.status]
					}}</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除此客户吗？" @confirm="deleteModule(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="orderList">
	import Form from './form.vue'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../customerInfo/api'
	const searchFormState = ref({
		username: ''
	})
	const formRef = ref()
	const searchFormRef = ref()
	const tableRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '用户名称',
			dataIndex: 'username',
			width: '200px',
			fixed: 'left',
			ellipsis: { showTitle: true }
		},
		{
			title: '服务模块',
			dataIndex: 'name',
			width: 200
		},
		{
			title: '开始时间',
			dataIndex: 'start_time',
			width: 200
		},
		{
			title: '结束时间',
			dataIndex: 'end_time',
			width: 200
		},
		{
			title: '状态',
			dataIndex: 'status',
			width: 100
		},
		{
			title: '创建时间',
			dataIndex: 'create_time',
			width: 200
		},
		{
			title: '修改时间',
			dataIndex: 'modified_time',
			width: 200
		},
		{
			title: '操作',
			dataIndex: 'action',
			fixed: 'right',
			align: 'center',
			width: '200px'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const total = ref(0)

	const mmoduleOptions = [
		{
			label: '基础版本实时人口投射',
			value: 1
		},
		{
			label: '高精版本实时人口投射',
			value: 2
		},
		{
			label: '人口投射分析',
			value: 3
		},
		{
			label: '全域人口投射分析',
			value: 4
		},
		{
			label: 'app可视化行为',
			value: 5
		},
		{
			label: '人口迁徙分析',
			value: 6
		},
		{
			label: '旅游行为分析',
			value: 7
		},
		{
			label: '全域人口投射系统',
			value: 8
		}
	]
	const loadData = (parameter) => {
		return api.modulePage(Object.assign(parameter, searchFormState.value)).then((res) => {
			total.value = res.total + 1
			return {
				current: parameter.pageNow,
				records: res.record,
				total: res.total
			}
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteModule = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		api.moduleDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}

	const userOptions = ref([])

	const loadOther = () => {
		return userApi.modulePage({ pageNow: 1, pagesize: 9999 }).then((res) => {
			userOptions.value = res.record.map((item) => {
				return {
					value: item.username,
					label: item.username
				}
			})
		})
	}
	loadOther()
</script>
